<?php
/**
 * Created by PhpStorm.
 * Author: i@fedax.cn
 * Date: 2018/1/5
 * Time: 17:53
 */

namespace app\admin\controller;
use think\Controller;
use think\facade\Cache;

use app\admin\model\Admin as AdminModel;
/**
 * 基础控制器
 * Class Base
 * @package app\admin\controller
 */
class Base extends Controller
{
    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login()
    {

        if(request()->isPost()){
            $data=array();
            $data['username']=input('username');
            $data['password']=input('password');
            $data['captcha']=input('captcha','');
            $validate = validate('User');
            if(!$validate->scene('login')->check($data)){
                $this->error($validate->getError());
            };
            $adminModel = new AdminModel;
            $modelReturn = $adminModel->loginByUserNameAndPassword(input('username'),input('password'));
            if($modelReturn['ret']==200){
                $this->success(lang('login') . lang('success'),'/');
            }
            $this->error(lang('login') . lang('false') . '-' . $modelReturn['msg']);
        }
        return view();
    }
    /**
     * 退出登陆
     */
    public function logout()
    {
        session('admin_token', null);
        $this->redirect('/');
    }

    /**
     * 清理缓存
     * @return \think\response\Json
     */
    public function delCache(){
        if (Cache::clear()) {
            $this->success(lang('operate') . lang('success'));
        } else {
            $this->error(lang('operate') . lang('false'));
        }
        return redirect('/Miss');
    }
}